package com.elitesland.demo.controller.backend.itm;

import com.elitesland.core.base.ApiResult;
import com.elitesland.core.config.argument.ImportExcel;
import com.elitesland.core.util.excel.ExcelExportUtil;
import com.elitesland.core.util.excel.support.ExportColumnParam;
import com.elitesland.demo.controller.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * .
 *
 * @author Kaiser（wang shao）
 * @date 2021-03-09
 */
@RestController
@RequestMapping(value = "/itm/item")
public class ItmItemController extends BaseController {

    @PostMapping("/import")
    public ApiResult<String> testUpload(@ImportExcel(headerRow = 1) List<Map<String, Object>> importDataList) {
        logger.info("导入数据：{}", importDataList);

        return ApiResult.ok();
    }

    @GetMapping(value = "/export")
    public ApiResult<String> testExport(HttpServletResponse response) throws Exception {
        List<Map<String, Object>> dataList = new ArrayList<>(64);
        dataList.add(Map.of("username", "张三", "age", 3));
        dataList.add(Map.of("username", "李四", "age", 2));
        dataList.add(Map.of("username", "王五", "age", 33));
        dataList.add(Map.of("username", "赵六", "age", 12));

        List<ExportColumnParam> fieldList = List.of(
                new ExportColumnParam("username", "姓名"),
                new ExportColumnParam("age", "性别")
        );


        ExcelExportUtil.instance(response, "0309")
                .fields(fieldList)
                .write("人员名单", dataList)
        ;
        return null;
    }
}
